import type { IQuery } from '@/api/config/types';
import type { FormColumn } from '@/types/form.message';
import type { FormConfig } from '@/types/system';
import { useCloned } from '@vueuse/core';
import { nanoid } from 'nanoid';

export const useQuery = <T>(query?: IQuery<T>): Ref<IQuery<T>> => {
  const queryObj = {
    pageNum: 1,
    pageSize: 10,
    orderByColumn: 'create_time',
    isAsc: 'desc',
  };
  // @ts-ignore
  return ref(Object.assign(queryObj, query));
};
export const useAxQuery = <T>(config?: T) => {
  const data = {
    pageSize: 15,
    pageNum: 1,
    total: 0,
  };
  return Object.assign(data, config);
};

export const useAxForm = <T>(config: T) => {
  const form = ref<T>({ ...config });
  const formRef = ref();

  let reset = () => {
    console.log('清空', formConfig.value.form);
    // @ts-ignore
    formConfig.value.form = { ...config };
  };
  let formConfig = ref({
    raw: { ...config },
    form: form.value,
    display: false,
    formRef,
    reset,
  });
  return formConfig;
};

type TableConfig<T> = {
  query: any;
  moduleName: string;
  rowKey: string;
  contextmenu: any[];
  viewMode: 'table' | 'card';
  loading: boolean;
  data: T[];
  keys: (string | number)[];
  reset: () => void;
};

export const useAxTable = <T>(config: Partial<TableConfig<T>>): Ref<TableConfig<T>> => {
  let obj: Partial<TableConfig<T>> = {
    data: [],
    loading: false,
    keys: [],
    viewMode: 'table',
    query: undefined,
  };
  let objRef = ref(Object.assign(obj, config));
  obj.reset = () => {
    objRef.value.query = config.query;
  };
  // @ts-ignore
  return objRef;
};

